package cn.libinal.scloud.winter.controller;

import cn.libinal.scloud.base.common.web.DataPageRequest;
import cn.libinal.scloud.base.common.web.ResultPage;
import cn.libinal.scloud.winter.domain.ProductVo;
import cn.libinal.scloud.winter.entity.Product;
import cn.libinal.scloud.winter.service.ProductService;
import com.querydsl.core.types.Predicate;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author libinal
 * created at 2018/11/3 19:34
 */
@RestController
@RequestMapping("product")
public class ProductController {
    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    /**
     * 分页查询
     * @param predicate 查询参数
     * @param pageRequest 分页参数
     * @return ResultPage
     */
    @GetMapping(name = "分页查询商品列表", path = "findByPaging")
    public ResultPage<ProductVo> findByPaging(@QuerydslPredicate(root = Product.class, bindings = ProductService.class) Predicate predicate, DataPageRequest pageRequest) {
        return productService.queryByPaging(predicate, pageRequest);
    }

    @PostMapping(name = "新增商品信息", path = "create")
    public void create(Product product) {
        productService.create(product);
    }

    @PostMapping(name = "更新商品信息", path = "update")
    public void update(Product product) {
        productService.update(product);
    }

    @GetMapping(name = "根据id查询商品信息", path = "findById")
    public Product findById(@RequestParam String id) {
        return productService.findById(id);
    }

    @PostMapping(name = "根据id删除商品信息", path = "delete")
    public void delete(@RequestParam String id) {
        productService.delete(id);
    }
}
